package core;

import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.security.Key;
import java.util.Base64;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.concurrent.TimeUnit;

public class main {

    private static Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) throws InterruptedException {
        while(true){
            System.out.println("------------欢迎使用对称加密程序------------");
            System.out.println("请选择要使用的功能: ");
            System.out.println("[1]: 加密\n[2]: 解密\n[3]: 退出程序");

            int selectedItem = 100;     // default Value

            try{
                selectedItem = scanner.nextInt();
            }catch(InputMismatchException e){
                System.out.println(e.toString());
            }finally {
                scanner.nextLine();
            }

            switch(selectedItem){
                case 1:
                    encode();break;
                case 2:
                    decode();break;
                case 3:
                    System.out.println("[谢谢使用, 两秒后将关闭程序, 再见！]");
                    TimeUnit.SECONDS.sleep(2);
                    System.exit(0);
                default:
                    System.out.println("[请输入有效数字！]");
            }
        }
    }

    private static void encode(){
        System.out.println("1.请输入要加密的原文");
        String text = scanner.nextLine();
        System.out.println("2.请输入16位密钥");
        String password = scanner.next();

        while(password.length() != 16){
            scanner.nextLine();
            System.out.println("[密钥长度需为16位!]");
            password = scanner.next();
        }

        // key
        Key key = new SecretKeySpec(password.getBytes(), "AES");

        // 加密 -> Base64编码
        byte[] result = null;
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            result = cipher.doFinal(text.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
        }

        byte[] encode = Base64.getEncoder().encode(result);
        System.out.println("加密后的密文:\n" + new String(encode));
    }

    private static void decode() {
        System.out.println("1.请输入要解密的密文");
        String encrptText = scanner.nextLine();
        System.out.println("2.请输入16位密钥");
        String password = scanner.next();

        while(password.length() != 16){
            scanner.nextLine();
            System.out.println("[密钥长度需为16位!]");
            password = scanner.next();
        }

        // key
        Key key = new SecretKeySpec(password.getBytes(), "AES");

        // Base64解码 -> 解密
        byte[] result = null;
        byte[] decode = Base64.getDecoder().decode(encrptText.getBytes());
        try {
            Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
            cipher.init(Cipher.DECRYPT_MODE, key);
            result = cipher.doFinal(decode);
        } catch (Exception e) {
            e.printStackTrace();
        }

        System.out.println("解密后的原文:\n" + new String(result));
    }
}
